home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 480 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: news.sprintlink.net!datalytics!news
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Exceptions vs. assertions
  5. Date: 4 Jan 1996 17:08:44 GMT
  6. Organization: Datalytics, Inc
  7. Message-ID: <4ch1is$ldg@gold.datalytics.com>
  8. References: <4al1hn$73e@news.nstn.ca> <4bpt9b$cb5@news2.ios.com>
  9. NNTP-Posting-Host: pc071.datalytics.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. lalit@gramercy.ios.com (lalit gidwani) wrote:
  16. >Pierre G. Boutquin (nstn181a@fox.nstn.ca) wrote:
  17. >: In <4aji1j$1o3@ricrdc1.rdc.ricoh.co.jp>, Bryan Larsen writes:
  18. >: >Thanks to every
  19. >: who replied.
  20. >: >
  21. >: >To sum up what I've learned -- feel free to correct me :)
  22. [snip]
  23. >: >2)  Exceptions are used for normal error handling, such as allocation 
  24. [snip]
  25. >: Exceptions are used to catch catastrophic errors -- such as 
  26. >: allocation failure.  Errors that will "normally" occur in a 
  27. >: program may better be implemented in a different fashion, 
  28. >: since exceptions don't come cheaply...
  29. [snip]
  30. >Someone needs to point me to a proper architecture for using Exceptions.
  31. >Several times I have come across articles that seem to apply that
  32. >any time there is an error condition, an exception should be used.
  33. >One of their arguments for doing this is that the program does
  34. >not have to check for the return value of functions. The exception
  35. >handling mechanism can be used instead. Since I have run into
  36. >a lot of code where a lot of erro-checking has to be done I thought
  37. >that exceptions would be helpful. However, the the note that appears
  38. >above implies that C++ exception-handling should be used only
  39. >for catastrophic errors.
  40. >
  41. >LG
  42. >
  43.  
  44. Exceptions aren't free.  Typically, each try block you code 
  45. causes a performance hit, so limit the number of try blocks 
  46. you use the best you can.  When you throw an exception, much 
  47. occurs behind the scenes.  Throwing an exception is more 
  48. expensive (in performance terms) than a simply function 
  49. return.  If you can throw an exception for several different 
  50. reasons, you may still need to decode the reason in the catch 
  51. block, so that isn't any different from decoding an error code 
  52. returned by a function.
  53.  
  54. As a result, you should limit your use of exceptions.  Don't 
  55. be afraid of them, but don't throw them when a simple result 
  56. code would suffice, unless the sort of error you're reporting 
  57. is quite likely going to unwind a great deal of functions 
  58. calls from the stack.  Such an error is probably of the 
  59. "catastrophic" variety anyway.
  60.  
  61. For example, a disk full condition is likely to unwind 
  62. numerous functions back to some common point at which the 
  63. program notifies the user of the condition and asks how to 
  64. proceed.  The program may allow the user to repeat the action 
  65. (hopefully after the user deleted some files from the full 
  66. volume).  You could return disk full as an error code and pass 
  67. it back the call chain until you reach the right point in the 
  68. code to prompt the user.  Since there aren't likely to be many 
  69. functions in the call stack that can handle it, "disk full" is 
  70. a good candidate for an exception.
  71.  
  72. On the other hand, a more temporary failure might not be 
  73. considered catastrophic, and could be handled closer to the 
  74. troubled function call.  It really is subjective.  You can't 
  75. even say, "I'm not sure what's a bad use of exceptions, but 
  76. I'll know it when I see it."
  77.  
  78. -- 
  79. Robert Stewart        | My opinions are usually my own.
  80. Datalytics, Inc.
  81. (513)226-7700
  82. stew@datalytics.com
  83.  
  84.  
  85.